﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using System.Reflection;



namespace SharpOffice
{
    
    public partial class Form1 : Form
    {
        
        private static SQL sql = new SQL(); //SQL class object
        ResourceManager res_man;  //resource manager object 

        public Form1()
        {
            InitializeComponent();
            searchBox.TextChanged += new EventHandler(searchBox_TextChanged); //event handler created for the searchBox,
                                                                              //because can't be bothered to find where should click to auto create it :D
            
        }
        /// <summary>
        /// Loads the form
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">Event argument</param>

        private void Form1_Load(object sender, EventArgs e)
        {
            
            fintoolStripMenuItem.Checked = false;
            deutoolStripMenuItem.Checked = false;
            engtoolStripMenuItem.Checked = true;           
            res_man = new ResourceManager("SharpOffice.Resource.Res.eng" , typeof(Form1).Assembly); // sets the english language on in load 
            language(); //loads the language text values
            loadSQLData(); //loads the database
            
        }      
       


        /// <summary>
        /// Pressing of this button refreshes the datagridview. Uses loadSQLData method.
        ///       
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">Event argument</param>
        /// 
        private void button1_Click(object sender, EventArgs e)
        {

            loadSQLData();            

         }

        /// <summary>
        /// Sets the dataset from SQL class as the data source of the dataGridView. Also formates the grid.
        /// </summary>
        private void loadSQLData()
        {
            
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = sql.selectQuery();            // sets the the dataset from database as the data source of the datagrid
            dataGridView1.Columns["cust_ID"].Visible = false;       // Hides the autoincremented customer_id number, which is not relevant for user.            
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;

            // Custom background color settings for dataGridView cells
            dataGridView1.RowsDefaultCellStyle.BackColor = Color.White;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            // Custom background color settings for the selected dataGridView cell
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.White;

            //custom names for the header cells
            //dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].HeaderCell.Value = res_man.GetString(@"first_name");
            dataGridView1.Columns[2].HeaderCell.Value = res_man.GetString(@"surname");
            dataGridView1.Columns[3].HeaderCell.Value = res_man.GetString(@"address");
            dataGridView1.Columns[4].HeaderCell.Value = res_man.GetString(@"postcode");
            dataGridView1.Columns[5].HeaderCell.Value = res_man.GetString(@"postarea");
            dataGridView1.Columns[6].HeaderCell.Value = res_man.GetString(@"phone");
            dataGridView1.Columns[7].HeaderCell.Value = res_man.GetString(@"email");
            dataGridView1.Columns[8].HeaderCell.Value = res_man.GetString(@"lastvisit");

        }

        /// <summary>
        /// Enables editing of the dataGridView.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">Event argumen</param>
        /// 
        private void new_btn_Click(object sender, EventArgs e)
        {

            dataGridView1.ReadOnly = false;
            dataGridView1.CurrentRow.Cells[8].ReadOnly = true;
            save_btn.Enabled = true;
            new_btn.Enabled = false;
            delete_btn.Enabled = false;
            button1.Enabled = false;

        }

        /// <summary>
        /// Saves changes of the dataGridView and calls sql.updateData() to save the changes to the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">Event argumen</param>
        /// 
        private void save_btn_Click(object sender, EventArgs e)
        {
            dataGridView1.CurrentRow.Cells[8].Value = DateTime.Now;
            sql.updateData();
            dataGridView1.ReadOnly = true;            
            button1.Enabled = true;
            save_btn.Enabled = false;
            new_btn.Enabled = true;
            delete_btn.Enabled = true;
        }

        /// <summary>
        /// Deletes the selected row from database. Once again by calling the sql.updateData() function.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">Event argumen</param>
        /// 
        private void delete_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(res_man.GetString(@"delete_info"), res_man.GetString("delete_btn_text"), MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                sql.updateData();
                save_btn.Enabled = false;
                new_btn.Enabled = true;
                delete_btn.Enabled = true;
            }
        }

        /// <summary>
        /// Closes the Application.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">Event argumen</param>
        ///        
        private void button2_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }
       



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       
        /// <summary>
        /// Sets the application language to Finnish
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void fintoolStripMenuItem_Click(object sender, EventArgs e)
        {
            fintoolStripMenuItem.Checked = true;
            deutoolStripMenuItem.Checked = false;
            engtoolStripMenuItem.Checked = false; 
            res_man = new ResourceManager("SharpOffice.Resource.Res.fin" , typeof(Form1).Assembly);
            loadSQLData();            
            language();
            
        }


        /// <summary>
        /// Sets the application language to English
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void engtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            fintoolStripMenuItem.Checked = false;
            deutoolStripMenuItem.Checked = false;
            engtoolStripMenuItem.Checked = true;
            res_man = new ResourceManager("SharpOffice.Resource.Res.eng", typeof(Form1).Assembly);
            loadSQLData();
            language();

          
        }


        /// <summary>
        /// Sets the application language to Germany
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deutoolStripMenuItem_Click(object sender, EventArgs e)
        {
            fintoolStripMenuItem.Checked = false;
            deutoolStripMenuItem.Checked = true;
            engtoolStripMenuItem.Checked = false;
            res_man = new ResourceManager("SharpOffice.Resource.Res.deu", typeof(Form1).Assembly);
            loadSQLData();
            language();          

        }

        /// <summary>
        /// Loads the language text values from resource file
        /// </summary>
        private void language()
        {
            LanguagetoolStripMenuItem.Text = res_man.GetString("Language_menu");
            delete_btn.Text = res_man.GetString("delete_btn_text");
            new_btn.Text = res_man.GetString("new_btn_text");
            save_btn.Text = res_man.GetString("save_btn_text");
            button1.Text = res_man.GetString("button1_text");
            button2.Text = res_man.GetString("button2_text");
            searchLabel.Text = res_man.GetString("search_text");
        }

        private void LanguagetoolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void searchBox_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Filters the datagridview with text from searchBox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "first_name like '%" + searchBox.Text + "%' OR surname like '%" + 
                    searchBox.Text + "%' OR address like '%" + searchBox.Text + "%' OR postcode like '%" + searchBox.Text + "%' OR postarea like '%" + 
                    searchBox.Text + "%' OR phone like '%" + searchBox.Text + "%' OR email like '%" + searchBox.Text + "%' ";
    
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        
    }
}
